package com.yoyo.yyadsdk.activity;


import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;


import androidx.appcompat.app.AppCompatActivity;

import com.yoyo.ad.contract.AdContract;
import com.yoyo.ad.main.IAdBannerListener;
import com.yoyo.ad.main.IAdFactory;
import com.yoyo.ad.main.YoYoAdManager;
import com.yoyo.yyadsdk.R;
import com.yoyo.yyadsdk.bean.dao.AdvScreenEntity;
import com.yoyo.yyadsdk.config.PullFeature;
import com.yoyo.yyadsdk.util.AdvUtils;
import com.yoyo.yyadsdk.util.DensityUtil;
import com.yoyo.yyadsdk.widget.LockBackgroundView;

import static com.yoyo.yyadsdk.config.Constants.LOCK;


public class LockActivity extends AppCompatActivity {

    private static final String TAG = "LockActivity";
    private FrameLayout mBannerContainer;
    private Context mContext;
    protected int mWidth;
    protected int mHeight;
    private IAdFactory mAdFactory;

    public static void launch(Context context, AdvScreenEntity advScreen) {
        if (context != null && advScreen != null)
            context.startActivity(new Intent(context, LockActivity.class)
                    .putExtra("advEntity", advScreen)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //四个标志位顾名思义，分别是锁屏状态下显示，解锁，保持屏幕长亮，打开屏幕。这样当Activity启动的时候，它会解锁并亮屏显示。
        beforeInit();
        setContentView(R.layout.activity_lock);
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        mWidth = outMetrics.widthPixels;
        mHeight = outMetrics.heightPixels;
        mContext = this;
        mBannerContainer = findViewById(R.id.banner_contains);
        LockBackgroundView parent = findViewById(R.id.parent);
        parent.setSwipeBackListener(new LockBackgroundView.SwipeBackFinishActivityListener(this));
        mAdFactory = YoYoAdManager.getAdFactory(mContext, new IAdBannerListener() {
            @Override
            public void adShow(int requestCode) {

            }

            @Override
            public void adClick(int requestCode) {
                finish();
            }

            @Override
            public void adDismissed(int requestCode) {
                finish();
            }

            @Override
            public void adFail(int requestCode, String arg0) {
                Log.e(TAG, arg0);
                finish();
            }
        });
        YoYoAdManager.getConfig(mContext, new AdContract.View() {
            @Override
            public void onAdSuccess() {
                int width = mWidth - DensityUtil.dp2px(mContext, 20);
                mAdFactory.getBanner(LOCK, 1, mBannerContainer, width, (int) (width / 6.4f));
            }

            @Override
            public void onAdFail(String err) {
                Log.e(TAG, err);
                finish();
            }
        });
        AdvScreenEntity advEntity = getIntent().getParcelableExtra("advEntity");
        AdvUtils.recordAdvExposed(advEntity.getId(), PullFeature.ADV_LOCK_SCREEN);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAdFactory.destroy();
    }

    private void beforeInit() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED //锁屏状态下显示
//                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD //解锁
//                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON //保持屏幕长亮
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); //打开屏幕
        Drawable wallPaper = WallpaperManager.getInstance(this).getDrawable();
        window.setBackgroundDrawable(wallPaper);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
//                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
//                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | (isDarkThemeStatusBar() ? View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR : View.SYSTEM_UI_FLAG_LAYOUT_STABLE));
            } else {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            }
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    @Override
    public void onBackPressed() {
    }

    public void close(View v) {
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.activity_out);
    }
}
